﻿using System;
using System.Windows;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZMCP.UI.PopWindows
{
    public partial class AddProduceProblem : EFViewCmes
    {
        public AddProduceProblem(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 事前操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    this.fdIsAudit.Value = "是";
                    this.fdProblemOrigin.Value = "0";//默认问题来源为调度提出
                    this.fdProblemStatus.Value = "0";
                    this.fdProblemTime.Value = DateTime.Now.ToString("HH:mm:ss"); ;//默认时间
                    this.fdProblemDate.Value = DateTime.Today.ToString("yyyy-MM-dd");//默认日期
                    this.fdCurrentDate.Value = DateTime.Today.ToString("yyyy-MM-dd");
                    this.fdIsAudit.Visibility = Visibility.Collapsed;                    
                    this.fdAuditDepartmentId.Visibility = Visibility.Collapsed;
                    this.fdAuditSuggest.Visibility = Visibility.Collapsed;
                    this.fdDealMethod.Visibility = Visibility.Collapsed;
                    this.fdFeedback.Visibility = Visibility.Collapsed;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    this.fdProblemOrigin.IsEnabled = false;
                    this.fdProblemDate.IsEnabled = false;
                    this.fdProblemTime.IsReadOnly = true;
                    this.fdIsAudit.Visibility = Visibility.Collapsed;                    
                    this.fdAuditDepartmentId.Visibility = Visibility.Collapsed;
                    this.fdAuditSuggest.Visibility = Visibility.Collapsed;
                    this.fdDealMethod.Visibility = Visibility.Collapsed;
                    this.fdFeedback.Visibility = Visibility.Collapsed;
                    break;
                case "deal":
                    base.Mode = OperationMode.Update;
                    this.Report.Visibility = Visibility.Collapsed;
                    this.fdAuditSuggest.Visibility = Visibility.Collapsed;
                    this.fdFeedback.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.InsertGrid.GetChildObjects<EFDataField>(null, true));
                    this.fdIsAudit.IsEnabled = true;
                    this.fdAuditDepartmentId.IsEnabled = true;
                    this.fdDealMethod.IsReadOnly = false;
                    this.fdIsAudit.eRegex = @"^.+$";
                    this.fdAuditDepartmentId.eRegex = @"^.+$";
                    this.fdDealMethod.eRegex = @"^.{1,250}$";
                    break;
                case "feedback":
                    base.Mode = OperationMode.Update;
                    this.Report.Visibility = Visibility.Collapsed;
                    this.fdAuditSuggest.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.InsertGrid.GetChildObjects<EFDataField>(null, true));
                    this.fdFeedback.IsReadOnly = false;
                    this.fdFeedback.eRegex = @"^.{1,250}$";
                    break;
                case "check":
                    base.Mode = OperationMode.Update;
                    this.Report.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.InsertGrid.GetChildObjects<EFDataField>(null, true));
                    this.fdAuditSuggest.IsReadOnly = false;
                    this.fdAuditSuggest.eRegex = @"^.{1,250}$";
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    this.Report.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.InsertGrid.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick("ZMCPB0", sender, e);
        }

        /// <summary>
        /// 上报按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReportClick(object sender, RoutedEventArgs e)
        {
            this.fdProblemStatus.Value = "1";
            this.CommitClick(sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 设置责任部门名称
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdBusinessDepartmentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                this.fdBusinessDepartment.Value = box.SelectedItem.GetType().GetProperty("orgName").GetValue(box.SelectedItem, null);
            }
        }

        /// <summary>
        /// 设置提出部门名称
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdSubmitorDepartmentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                this.fdSubmitorDepartment.Value = box.SelectedItem.GetType().GetProperty("orgName").GetValue(box.SelectedItem, null);
            }
        }

        /// <summary>
        /// 设置核查部门名称
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdAuditDepartmentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                this.fdAuditDepartment.Value = box.SelectedItem.GetType().GetProperty("orgName").GetValue(box.SelectedItem, null);
            }
        }

        /// <summary>
        /// 是否核查来判断核查部门是否必填
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdIsAudit_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                if(e.NewValue.Equals("是"))
                {
                    this.fdAuditDepartmentId.Visibility = Visibility.Visible;
                    this.fdAuditDepartmentId.eRegex = @"^.+$";
                }
                else
                {
                    this.fdAuditDepartmentId.Value = "";
                    this.fdAuditDepartment.Value = "";
                    this.fdAuditDepartmentId.Visibility = Visibility.Collapsed;
                    this.fdAuditDepartmentId.eRegex = "";
                }
            }
        }

    }
}
